<template>
  <h1>员工信息</h1>
  <el-row :gutter="10">

    <el-col :span="6">
      <el-input v-model="query.employeeData.employeeName" placeholder="请输入员工姓名" style="width: 200px;" />
    </el-col>

    <el-col :span="6">
      <el-select v-model="query.employeeData.departmentId"
                 :clearable="true"
                 placeholder="请选择部门">
        <el-option v-for="item in departments.data"
                   :key="item.departmentId"
                   :label="item.departmentName"
                   :value="item.departmentId" />
      </el-select>
    </el-col>

    <el-col :span="6">
      <el-select v-model="query.employeeData.gender"
                 :clearable="true"
                 placeholder="请选择性别">
        <el-option label="男" value="男" />
        <el-option label="女" value="女" />
      </el-select>
    </el-col>


    <el-col :span="6">
      <el-button type="primary" @click="getEmployeeData(query.employeeData)">搜索</el-button>
    </el-col>

  </el-row>
  <el-table :data="tableData.data" style="width: 100%;margin-top: 20px"
            :cell-style="cellStyle"
  >

    <el-table-column label="序号" type="index" width="80" align="center">
    </el-table-column>

    <el-table-column label="姓名" prop="employeeName" width="100" align="center">
    </el-table-column>


    <el-table-column label="科室" prop="departmentName" width="100" align="center">
    </el-table-column>


    <el-table-column label="薪资" prop="salary" width="100" align="center">

      <template #default="scope">
        <div>{{ (scope.row.salary / 100).toFixed(2) + "元" }}</div>
      </template>
    </el-table-column>

    <el-table-column label="职务" prop="position" width="120" align="center">
    </el-table-column>


    <el-table-column label="性别" prop="gender" width="80" align="center">
    </el-table-column>


    <el-table-column label="账号" prop="userAccount" width="160" align="center">
    </el-table-column>

    <el-table-column label="生日" width="130" align="center">
      <template #default="scope">
        <div style="display: flex; align-items: center;">
          <el-icon>
            <timer />
          </el-icon>
          <span style="margin-left: 10px">{{ scope.row.bithday }}</span>
        </div>
      </template>
    </el-table-column>

    <el-table-column label="入职日期" width="130px" align="center">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <el-icon>
            <timer />
          </el-icon>
          <span style="margin-left: 10px">{{ scope.row.createTime.slice(0, 10) }}</span>
        </div>
      </template>
    </el-table-column>

    <el-table-column label="操作" width="200px" align="center">
      <template #default="scope">
        <el-button
          style="width: 60px;"
          size="small"
          @click="handleEdit(scope.row)"
        >修改
        </el-button>
        <el-button
          style="width: 60px;"
          size="small"
          type="danger"
          v-model:current-page="query.employeeData.pages"
          v-model:age-sizes="query.employeeData.size"
          @click="handleDelete(scope.row)"
        >删除
        </el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <!--分页组件-->
  <div>
    <el-pagination
      small
      layout="total, prev, pager, next, jumper"
      :total="tableData.total"
      class="pagiantion "
      @current-change="pageChange"
      v-model:current-page="query.employeeData.pages"
      v-model:page-size="query.employeeData.size"
    />
  </div>
</template>

<script lang="js" setup>
import { Timer } from "@element-plus/icons-vue";
import { onMounted, reactive } from "vue";
import { getEmployees, deleteEmployeeById, getDepartmentNameId } from "@/utils/api";
import { ElMessage, ElMessageBox } from "element-plus";
import { useRouter } from "vue-router";
import { useStore } from "vuex";
const store = useStore();
const router = useRouter();
//展示数据
const tableData = reactive({
  data: [],
  total: 0,
  current: 1, //当前页数
  size: 10   //每页显示几条数据
});
const departments = reactive({
  data: []
});
//请求参数
const query = reactive({
  employeeData: {
    "departmentId": "",
    "gender": "",
    "name": "",
    "pages": tableData.current,
    "phone": "",
    "position": "",
    "roler": "",
    "salaryType": "",
    "size": tableData.size,
    "userAccount": ""
  }
});


//组件加载时触发
onMounted(() => {
  getEmployeeData(query.employeeData);
  getDepartmentOptionData();
});

//获取数据
const getEmployeeData = async (requestData) => {
  let res = await getEmployees(requestData);
  tableData.data = res.data;
  tableData.total = res.total;
  tableData.current = res.current;
};

//获取部门菜单的数据
const getDepartmentOptionData = async () => {
  let res = await getDepartmentNameId();
  departments.data = res.data;
};

//分页的页码变化时触发
const pageChange = () => {
  // console.log(query.employeeData.pages);
  getEmployeeData(query.employeeData);
};

//修改回调
const handleEdit = (row) => {
  router.push({
    path: "/home/updatemps",
    query: { id: row.employeeId }
  });
};

//删除
const handleDelete = (row) => {
  ElMessageBox.confirm(
    "确定要删除吗?",
    "警告",
    {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning"
    }
  )
    .then(async () => {
      let res = await deleteEmployeeById(row.employeeId);
      if (res.data) {
        ElMessage({
          type: "success",
          message: "删除成功"
        });
      } else {
        ElMessage({
          type: "error",
          message: "删除成功"
        });
      }
      getEmployeeData(query.employeeData);

    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消删除"
      });
    });
};


// 根据data返回的每一行的数据判断,再修改这一行的样式
const cellStyle = (data) => {
  if (data.row.employeeId == store.state.user.userInfo.employeeId) {
    return {
      color: "#000",
      textAlign: "center",
      background: "#409EFF",
      fontWeight: 700
    };
  }
};
</script>

<style scoped>
</style>